package org.snail.message.service.listener;

import com.alibaba.fastjson.JSON;
import lombok.extern.log4j.Log4j;
import org.snail.message.service.option.GroupMessageOption;
import org.snail.other.bean.GroupNotificationDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@Component("groupMessageSenderListener")
@Log4j
public class GroupMessageSenderListener implements MessageListener {

    @Autowired
    private GroupMessageOption groupMessageOption;

    @Override
    public void onMessage(Message message) {
        try{
            sendGroupMessage(message);
        }catch (Exception e){
            log.error(e.getMessage());
        }

    }

    private void sendGroupMessage(Message message) throws JMSException {
        log.info("[GroupMessageSenderListener.onMessage]:begin onMessage.");
        TextMessage textMessage = (TextMessage) message;
            //获取数据
            String jsonStr = textMessage.getText();
            log.info("[GroupMessageSenderListener.onMessage]:receive message is," + jsonStr);
            if (jsonStr != null) {
                GroupNotificationDTO groupNotificationDTO = JSON.parseObject(jsonStr, GroupNotificationDTO.class);
                groupMessageOption.sendGroupMessage(groupNotificationDTO);
            }
    }

}